package com.xteam.youin_supplier.fragment;


import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonSyntaxException;
import com.xteam.youin_supplier.MainActivity;
import com.xteam.youin_supplier.R;
import com.xteam.youin_supplier.config.GlobalData;
import com.xteam.youin_supplier.config.RequestAddr;
import com.xteam.youin_supplier.model.BaseModel;
import com.xteam.youin_supplier.model.SalesManger;
import com.xteam.youin_supplier.util.http.AjaxCallBack;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * 进销存管理
 */
@EFragment(R.layout.fragment_sales)
public class SalesFragment extends BaseFragment {

    @ViewById(R.id.back_btn)
    ImageButton backBtn;

    @ViewById(R.id.toptitle_title)
    TextView topTitle;

    @ViewById(R.id.month_in_oil_num_content)
    TextView monthOilNum;
    @ViewById(R.id.month_in_sales_num_content)
    TextView salesNum;
    @ViewById(R.id.month_book_inventory_content)
    TextView bookInventory;
    @ViewById(R.id.month_total_version_content)
    TextView totalVersion;


    @AfterViews
    @Override
    void initView() {
        backBtn.setVisibility(View.VISIBLE);
        topTitle.setText(getResources().getString(R.string.in_book_inventory));

        loadData();
    }

    private void loadData() {
        requestParams = "supplier_id="+ GlobalData.userInfo.getData().getSupplier_id();
        requestUrl = RequestAddr.stockManage + requestParams;
        xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onSuccess(Object object) {
                super.onSuccess(object);
                dealData(object);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    SalesManger salesManger;
    private void dealData(Object object) {
        try {
            salesManger = gson.fromJson(object.toString(), SalesManger.class);
        } catch (JsonSyntaxException e) {
            baseModel = gson.fromJson(object.toString(), BaseModel.class);
            Toast.makeText(getActivity(), baseModel.getMsg(), Toast.LENGTH_SHORT).show();
            return;
        }
         monthOilNum.setText(salesManger.getData().getAddamount());
         salesNum.setText(salesManger.getData().getSellamount());
         bookInventory.setText(salesManger.getData().getAmount());
         totalVersion.setText(salesManger.getData().getRealamount());
    }

    @Click({R.id.back_btn, R.id.edit_btn})
    @Override
    void Click(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getInstance().getSupportFragmentManager().popBackStack();
                break;
            case R.id.edit_btn:
                MainActivity.getInstance().switchFragment(this, new InfomationImportFragment_());
                break;
        }
    }


}
